package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.ModuleTreemain;
import com.ruoyi.system.domain.StringId;
import com.ruoyi.system.domain.comment.EngComment;
import com.ruoyi.system.domain.reports.ClassReport;
import com.ruoyi.system.service.IModuleTreemainService;
import com.ruoyi.system.service.IModuleVolumeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ModuleEngInfo;
import com.ruoyi.system.service.IModuleEngInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工程信息Controller
 *
 * @author ruoyi
 * @date 2023-07-12
 */
@RestController
@RequestMapping("/system/enginfo")
public class ModuleEngInfoController extends BaseController {
    @Autowired
    private IModuleEngInfoService moduleEngInfoService;
    @Autowired
    private IModuleVolumeInfoService moduleVolumeInfoService;
    @Autowired
    private IModuleTreemainService moduleTreemainService;

    /**
     * 查询工程信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:enginfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModuleEngInfo moduleEngInfo) {
        startPage();
        List<ModuleEngInfo> list = moduleEngInfoService.selectModuleEngInfoList(moduleEngInfo);
        return getDataTable(list);
    }

    /**
     * 查询文件信息通过id
     */
    @PostMapping("/getEngInfoById")
    public AjaxResult getEngInfoById(@RequestBody StringId stringId) {
        if (stringId.getIds() != null) {
            String[] stringList = stringId.getIds();
            String id = stringList[0];
            return success(moduleEngInfoService.selectModuleEngInfoById(id));
        } else {
            String id = stringId.getId();
            return success(moduleEngInfoService.selectModuleEngInfoById(id));
        }
    }

    /**
     * 导出工程信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:enginfo:export')")
    @Log(title = "工程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModuleEngInfo moduleEngInfo) {
        List<ModuleEngInfo> list = moduleEngInfoService.selectModuleEngInfoList(moduleEngInfo);
        ExcelUtil<ModuleEngInfo> util = new ExcelUtil<ModuleEngInfo>(ModuleEngInfo.class);
        util.exportExcel(response, list, "工程信息数据");
    }

    /**
     * 查询工程注释信息列表
     *
     * @return 工程信息集合
     */
    @PostMapping("/getEngCommentInfo")
    public List<EngComment> getEngCommentInfot() {
        return moduleEngInfoService.selectModuleEngComInfoList();
    }

    /**
     * 获取工程信息详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:enginfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@RequestBody ModuleEngInfo moduleEngInfo) {
        String id = moduleEngInfo.getId();
        return success(moduleEngInfoService.selectModuleEngInfoById(id));
    }

    /**
     * 新增工程信息
     */
    // @PreAuthorize("@ss.hasPermi('system:enginfo:add')")
    @Log(title = "工程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ModuleEngInfo moduleEngInfo) {
        return toAjax(moduleEngInfoService.insertModuleEngInfo(moduleEngInfo));
    }

    /**
     * 修改工程信息
     */
    // @PreAuthorize("@ss.hasPermi('system:enginfo:edit')")
    @Log(title = "工程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ModuleEngInfo moduleEngInfo) {
        return toAjax(moduleEngInfoService.updateModuleEngInfo(moduleEngInfo));
    }

    /**
     * 删除工程信息
     */
    // @PreAuthorize("@ss.hasPermi('system:enginfo:remove')")
    @Log(title = "工程信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(moduleEngInfoService.deleteModuleEngInfoByIds(ids));
    }

    /**
     * 删除工程信息通过stringId
     */
    @PostMapping("/removeEngInfoById")
    public AjaxResult removeEngInfoById(@RequestBody StringId stringId) {
        if (stringId.getIds() != null) {
            String[] ids = stringId.getIds();
            return toAjax(moduleEngInfoService.deleteModuleEngInfoByIds(ids));
        } else {
            String id = stringId.getId();
            return toAjax(moduleEngInfoService.deleteModuleEngInfoById(id));
        }
    }

    // @PreAuthorize("@ss.hasPermi('system:enginfo:query')")
    @GetMapping(value = "/getYearEngVolumeDatas/{year}")
    public Map<Object, Object> getYearEngVolumeDatas(@PathVariable("year") Integer year) {
        Map<Object, Object> ansMap = new HashMap<Object, Object>();
        // 1.get engInfo by year
        // 2.sort by year, get eng number sum & volume number sum
        int count = 5;
        int[] years = new int[count];
        int[] engNums = new int[count];
        int[] volumeNums = new int[count];
        for (int i = 0; i < count; i++) {
            int thisYear = year - i;
            years[i] = thisYear;
            List<ModuleEngInfo> engInfos = moduleEngInfoService.findByYear(thisYear);
            engNums[i] = engInfos.size();
            volumeNums[i] = 0;
            for (int j = 0; j < engInfos.size(); j++) {
                ModuleEngInfo engInfo = engInfos.get(j);
                try {
                    volumeNums[i] += Integer.parseInt(engInfo.getAnjuanshu());
                } catch (Exception e) {
                    volumeNums[i]++;
                }
            }
        }
        // 3.push
        ansMap.put("years", reverseData(years));
        ansMap.put("engNums", reverseData(engNums));
        ansMap.put("volumeNums", reverseData(volumeNums));
        return ansMap;
    }

    /**
     * 反转数组数据，为前端展示数据做处理
     * 
     * @param data
     * @return
     */
    private int[] reverseData(int[] data) {
        int[] ans = new int[data.length];
        for (int i = 0; i < data.length; i++) {
            ans[i] = data[data.length - 1 - i];
        }
        return ans;
    }

    @RequestMapping("/getClassDatas")
    public Map<Object, Object> getClassDatas() {
        List<ModuleTreemain> treemainList = moduleTreemainService.getFirstClass();
        Map<Object, Object> returnMap = new HashMap<Object, Object>();
        List<ClassReport> classReportList = new ArrayList<ClassReport>();
        for (ModuleTreemain treemain : treemainList) {
            ClassReport classReport = new ClassReport();
            classReport.setName(treemain.getIndexName());
            classReport.setValue(moduleVolumeInfoService.findMaxRowInClass(treemain.getId()));
            classReportList.add(classReport);
        }
        returnMap.put("reportdata", classReportList);
        return returnMap;
    }

    // @GetMapping("/getClassDatas")
    // public Map<Object, Object> getClassDatas() {
    // List<ModuleEngInfo> treemainList = moduleEngInfoService.getFirstClass();
    // int[] array = new int[18];
    // String[] variables = {
    // "A 综合类",
    // "B 城市勘测类",
    // "C 城市规划类",
    // "D 城市建设管理类",
    // "E 市政工程类",
    // "F 公用设施类",
    // "G 交通运输类",
    // "H 工业建筑类",
    // "I 民用建筑类",
    // "J 名胜古迹园林绿化类",
    // "K 环境保护类",
    // "L 城市建设科学研究类",
    // "M 县（村）镇建设类",
    // "N 人防、军事工程类",
    // "O 水利、防灾类",
    // "P 工程设计类",
    // "Q 地下管线类",
    // "R 声像类"
    // };
    // for(int i = 0; i < array.length; i++){
    // array[i] = 0;
    // }
    // for(ModuleEngInfo moduleEngInfo : treemainList){
    // Long engClassId = moduleEngInfo.getSysClass().getParentId(); //2
    // SysClass sysClass = new SysClass();
    // if(engClassId == 0){
    // sysClass.setClassId(moduleEngInfo.getSysClass().getClassId());
    // }
    // while(engClassId != 0){ //eng表中的classid不是根节点
    // sysClass = sysClassService.selectSysClassByClassId(engClassId); //找classid =
    // 2对应的行
    // engClassId = sysClass.getParentId(); //1
    // }
    // switch (sysClass.getClassId().intValue()) {
    // case 1:
    // array[0]++;
    // break;
    // case 15:
    // array[1]++;
    // break;
    // case 16:
    // array[2]++;
    // break;
    // case 37:
    // array[3]++;
    // break;
    // case 43:
    // array[4]++;
    // break;
    // case 44:
    // array[5]++;
    // break;
    // case 45:
    // array[6]++;
    // break;
    // case 46:
    // array[7]++;
    // break;
    // case 47:
    // array[8]++;
    // break;
    // case 48:
    // array[9]++;
    // break;
    // case 49:
    // array[10]++;
    // break;
    // case 50:
    // array[11]++;
    // break;
    // case 51:
    // array[12]++;
    // break;
    // case 52:
    // array[13]++;
    // break;
    // case 53:
    // array[14]++;
    // break;
    // case 54:
    // array[15]++;
    // break;
    // case 55:
    // array[16]++;
    // break;
    // case 312:
    // array[17]++;
    // break;
    // }
    // }
    // Map<Object, Object> returnMap = new HashMap<Object, Object>();
    // List<ClassReport> classReportList = new ArrayList<ClassReport>();
    // for (int i = 0; i < variables.length; i++) {
    // ClassReport classReport = new ClassReport();
    // classReport.setName(variables[i]);
    // classReport.setValue(array[i]);
    // classReportList.add(classReport);
    // }
    // returnMap.put("reportdata", classReportList);
    // return returnMap;
    // }
}
